<template>
  <!-- 搜索历史 -->
  <div>
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" :error.sync="error"
      error-text="加载失败，点击重新加载">
      <van-cell v-for="item in list" :key="item.art_id" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getSearchResults } from '@/api/search.js'
export default {
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      // 页码
      page: 1,
      // 每页显示几条
      per_page: 10,
      // 错误提示显示
      error: false
    }
  },
  props: {
    // 父组件输入框的值
    text: {
      type: String,
      require: true
    }
  },
  methods: {
    // 页面加载时触发
    async onLoad () {
      try {
        // 根据搜索框的值获取搜索结果
        const { data: res } = await getSearchResults({
          page: this.page,
          per_page: this.per_page,
          q: this.text
        })
        this.list.push(...res.data.results)
        // 结束本次加载
        this.loading = false
        // 判断是否还有需要加载的数据
        if (this.list.length === res.data.total_count) {
          // 全部加载完了
          this.finished = true
        } else {
          // 没加载完加载下一页
          this.page++
        }
      } catch (err) {
        this.error = true
        this.loading = false
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
